package com.yangyouli.unity_demo_server.container;

import org.apache.ibatis.session.SqlSession;

import com.yangyouli.unity_demo_server.util.ConnUtil;
import com.yangyouli.unity_demo_server.util.ThreadLocalHelper;

/**
 * SqlSession 持有者
 * @author 24279
 *
 */
public class SqlSessionHolder {
	private SqlSession sqlSession;
	
	private Boolean isTransaction;

	public SqlSessionHolder (SqlSession sqlSession) {
		this.sqlSession = sqlSession;
		reset();
	}
	
	public void reset () {
		isTransaction = false;
	}
	
	public SqlSession getSqlSession() {
		return sqlSession;
	}
	
	public void close () {
		this.sqlSession.close();
		ThreadLocalHelper.removeSqlSessionHolder(ConnUtil.getSqlSessionFactory());
	}
	
	/**
	 * 当前是否配置了事务管理
	 * @return
	 */
	public Boolean isTransaction() {
		return isTransaction;
	}

	public void setIsTransaction(Boolean isTransaction) {
		this.isTransaction = isTransaction;
	}
	
	public void rollback () {
		this.sqlSession.rollback();
	}
}
